<template>
  <transition name="fade">
    <div class="modal" v-show="shown" :style="{zIndex: index}" @click="clickBgToClose">
      <transition name="v-slide">
        <div ref="panel" class="modal-dialog" v-show="shown">
          <div class="modal-title">
            {{title || '无标题'}}
            <button v-if="showClose" type="button" class="btn btn-xs btn-link" @click="close"><i class="fa fa-times"></i></button>
          </div>
          <div class="modal-body">
            <slot></slot>
          </div>
        </div>
      </transition>
    </div>
  </transition>
</template>

<script>
  let initIndex = 2000
  let modalStack = []

  import _ from 'lodash'

  function closeModalByEsc (evt) {
    let keyCode = evt.keyCode ? evt.keyCode : evt.which
    if (keyCode === 27 && modalStack.length > 0) {
      let vm = modalStack[modalStack.length - 1]
      vm.close()
    }
  }

  function updateBodyStyle () {
    let body = _.head(document.getElementsByTagName('body'))
    if (body) {
//      body.style.overflow = modalStack.length > 0 ? 'hidden !important' : 'inherit'
      if (modalStack.length > 0) {
        body.style.setProperty('overflow', 'hidden', 'important')
      } else {
        body.style.setProperty('overflow', 'inherit')
      }
    }
  }

  export default {
    props: {
      title: {
        type: String
      },
      disableEscClose: {
        type: Boolean,
        default: false
      },
      staticModal: {
        type: Boolean,
        default: true
      },
      showClose: {
        type: Boolean,
        default: true
      }
    },
    data () {
      return {
        index: initIndex,
        shown: false
      }
    },
    methods: {
      open () {
        initIndex++
        this.index = initIndex
        this.shown = true
        if (!this.disableEscClose) {
          modalStack.push(this)
          window.removeEventListener('keyup', closeModalByEsc)
          window.addEventListener('keyup', closeModalByEsc)
        }
      },
      close () {
        this.shown = false
        if (!this.disableEscClose) {
          modalStack.splice(_.indexOf(modalStack, this), 1)
          if (modalStack.length === 0) {
            window.removeEventListener('keyup', closeModalByEsc)
          }
        }
      },
      clickBgToClose (e) {
        if (!this.staticModal && !this.$refs.panel.contains(e.target)) {
          this.close()
        }
      }
    },
    watch: {
      shown () {
        updateBodyStyle()
      }
    }
  }
</script>

<style lang="scss">
  $title-text-color: #333;
  $title-border-color: #d2d2d2;
  $close-button-color: #666;

  .modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    overflow: scroll;
    background: rgba(0, 0, 0, 0.1);
  }
  .modal-dialog {
    position: relative;
    margin-left: auto;
    margin-right: auto;
    margin-top: 120px;
    background: #fff;
    border-radius: 4px;
    min-width: 500px;
    max-width: 1024px;
  }
  .modal-title {
    position: relative;
    line-height: 48px;
    min-height: 48px;
    font-size: 16px;
    padding: 0 20px;
    color: $title-text-color;
    border-bottom: solid 1px $title-border-color;

    button {
      position: absolute;
      font-size: 14px;
      right: 10px;
      top: 12px;

      &.btn-link {
        color: $close-button-color;
      }
      &:hover, &:active {
        color: darken($close-button-color, 10%);
      }
    }
  }
  .modal-body {
    padding: 15px;
  }

  .v-slide-enter-active, .v-slide-leave-active {
    transition: all .5s;
    transform: translateY(0);
  }
  .v-slide-enter, .v-slide-leave-to /* .fade-leave-active in <2.1.8 */ {
    transform: translateY(-400px);
  }
</style>
